package com.evil.account.service.impl;

import com.evil.account.api.RemoteLoginService;
import com.evil.account.pojo.entity.Enterprise;
import com.evil.account.pojo.entity.User;
import com.evil.account.service.EnterpriseService;
import com.evil.account.service.TokenService;
import com.evil.account.service.UserService;
import com.evil.account.strategy.login.AuthServiceStrategy;
import com.evil.common.account.bo.AuthUserBO;
import com.evil.common.account.dto.LoginReqDTO;
import com.evil.common.account.dto.LogoutReqDTO;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.account.dto.user.UserEnterpriseBaseDTO;
import com.evil.common.account.util.LoginUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
public class LoginServiceImpl implements RemoteLoginService {

    private final LoginUtil loginUtil;

    private final AuthServiceStrategy authServiceStrategy;

    private final UserService userService;

    private final EnterpriseService enterpriseService;

    private final TokenService tokenService;

    /**
     * 登录
     *
     * @param loginReqDTO 参数
     */
    @Override
    public AuthUserBO login(LoginReqDTO loginReqDTO) {
        return authServiceStrategy.login(loginReqDTO);
    }

    /**
     * 登出
     *
     * @param logoutReqDTO logoutReqDTO
     */
    @Override
    public void logout(LogoutReqDTO logoutReqDTO) {
        // 删除token
        tokenService.delToken(logoutReqDTO.getToken());
    }

    /**
     * 复制登录信息
     *
     * @return token
     */
    @Override
    public String cpLoginToken(UserEnterpriseBaseDTO userEnterpriseDTO) {
        User user = userService.getBaseMapper().findById(userEnterpriseDTO.getUserId());
        Enterprise enterprise = enterpriseService.getBaseMapper().findById(userEnterpriseDTO.getEnterpriseId());
        return tokenService.create(user, enterprise);
    }

    /**
     * 获取当前登录用户id
     *
     * @return id
     */
    @Override
    public Long getLoginUserId() {
        return loginUtil.getLoginUserId();
    }

    /**
     * 获取当前登录用户
     *
     * @return user
     */
    @Override
    public UserBaseRespDTO getLoginUser() {
        return loginUtil.getLoginUserBase();
    }

    /**
     * 获取当前企业id
     *
     * @return user
     */
    @Override
    public Long getLoginEnterpriseId() {
        return loginUtil.getLoginEnterpriseId();
    }

    /**
     * 获取当前企业
     *
     * @return user
     */
    @Override
    public UserEnterpriseBaseDTO getLoginEnterprise() {
        return loginUtil.getLoginUserEnterprise();
    }
}
